import request from "../axios";
import axios from 'axios';
import { instance, plainAxios,instanceFlask }  from "../axios";
export const login = async (credentials) => {
  return instance.post('/auth/login', credentials); // credentials 包含 type 字段
};
//成功时需要返回user{id,username,身份认证用的token}
//data格式：{account: '',
// password: '',
// email: '',
// code: ''}

export const getRigisterCode = async (email) => {
  return instance.post('/auth/register_code',{ email });
}
export const getUserInfo = (token) => {
  return instance.get(`/auth/user/${token}`);
};
//示例数据
// return jsonify({
//             "username": "小明",
//             "avatar": "/static/avatar.jpg",
//             "level": 7,
//             "upload": "12TB",
//             "download": "8TB",
//             "ratio": 1.5,
//             "score": 8560,
//             "yangtao": 42
//         })
// export const register = (data) => {
//   const { code, ...body } = data;
//   return instance.post(`/auth/register?code=${encodeURIComponent(code)}`, body);
// };
export const register = (data) => {
  // 创建用户对象，排除验证码相关字段
  const userData = {
    username: data.username,
    email: data.email,
    password: data.password
  };
  
  return instance.post(`/auth/register`, userData, {
    params: {
      code: data.code,
      inviteCode: data.invitationCode // 注意参数名保持一致
    }
  });
};
//data格式；{username: '',
    // email: '',
    // password: '',
    // confirmPassword: '',
    // invitationCode: ''}
export const changePassword = (data) => {
  return instance.post(`/auth/forgot-password`, data, {
    params: {
      code: data.code,
      email: data.email,
      password:data.password
}});
}
export const submitLikes = (data) => {
  return instance.post(`/auth/setPreferences`,{ preferences: data })
}

// 获取用户余额/积分信息
export const getUserBalance = () => {
  return instance.get('/usercenter/balance');
}

// 生成邀请码
export const generateInviteCode = () => {
  return instance.post('/usercenter/generate-invite-code');
}

// 获取邀请码列表
export const getInviteCodes = (type = 'all') => {
  return instance.get(`/usercenter/my-invite-codes?type=${type}`);
}
export const getUserName = (id)=>{
    return instance.get(`/auth/other-user/${id}`)
}